/**
 * @(#)TestChannel.java, Nov 21, 2012. 
 * 
 * Copyright 2012 Yodao, Inc. All rights reserved.
 * YODAO PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.buaaer.net;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

/**
 *
 * @author wangheng
 *
 */
public class TestChannel {
    
    public static void main(String[] args) throws IOException {
        
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        ServerSocket serverSocket = serverSocketChannel.socket();
        serverSocket.bind(new InetSocketAddress(9876));
        serverSocketChannel.configureBlocking(false);
        
        Selector selector = Selector.open();
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
        System.out.println("before select");
        selector.select();//阻塞等待连接
//      selector.selectNow()();//不阻塞
        System.out.println("get connect");
        Set<SelectionKey> readyKeys = selector.selectedKeys();
        
        Iterator<SelectionKey> iterator = readyKeys.iterator();
        
        while (iterator.hasNext()) {
            SelectionKey selectionKey = iterator.next();
            iterator.remove();
            
            System.out.println("valid:" + selectionKey.isValid());
            System.out.println("acceptable:" + selectionKey.isAcceptable());
            System.out.println("connectable:" + selectionKey.isConnectable());
            System.out.println("readable:" +selectionKey.isReadable());
            System.out.println("writable:" + selectionKey.isWritable());
            
            if (selectionKey.isAcceptable()) {//普通socket连接
                ServerSocketChannel channel = (ServerSocketChannel)selectionKey.channel();
                SocketChannel client = channel.accept();
                System.out.println("accept client:" + client);
                client.configureBlocking(false);
                System.out.println("2");
                SelectionKey key2 = client.register(selector, SelectionKey.OP_WRITE);
                ByteBuffer buffer = ByteBuffer.allocate(20);
                byte[] b = new byte[]{'a','s','d','f','\r','\n'};
                buffer.put(b);
                buffer.flip();
                key2.attach(buffer);
                System.out.println("over");
                
            } else if (selectionKey.isWritable()) {
                System.out.println("else");
            }

            
            selectionKey.cancel();
            selectionKey.channel().close();
            try {
                Thread.currentThread().join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
        }
        
    }

}
